package com.ruikang.slidemenutest;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.ruikang.slidemenutest.entity.MyData;

import java.util.List;

/**
 * Created by：hcs on 2016/6/17 16:05
 * e_mail：aaron1539@163.com
 */
public class MyAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter {

    private List<MyData> data;
    private Context context;

    public MyAdapter(List<MyData> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return ((MyData)getItem(position)).type;
    }

    @Override
    public boolean isItemViewTypePinned(int viewType) {
        return viewType == MyData.SECTION;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        int itemViewType = getItemViewType(position);
        if(itemViewType == MyData.ITEM){
            ret = bindItemView(position, convertView, parent);
        }else if(itemViewType == MyData.SECTION){
            ret = bindTitleView(position, convertView, parent);
        }

        return ret;
    }

    private View bindTitleView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if(convertView == null){
            ret = LayoutInflater.from(context).inflate(R.layout.item_one,parent,false);
        }else{
            ret = convertView;
        }

        TitleViewHolder holder = (TitleViewHolder) ret.getTag();

        if(holder == null){
            holder = new TitleViewHolder();
            holder.title = (TextView) ret.findViewById(R.id.text_one);
            ret.setTag(holder);
        }

        holder.title.setText(data.get(position).text);

        return ret;
    }

    private View bindItemView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if(convertView == null){
            ret = LayoutInflater.from(context).inflate(R.layout.item_two,parent,false);
        }else{
            ret = convertView;
        }

        ContentViewHolder holder = (ContentViewHolder) ret.getTag();

        if(holder == null){
            holder = new ContentViewHolder();
            holder.cont = (TextView) ret.findViewById(R.id.text_two);
            ret.setTag(holder);
        }

        holder.cont.setText(data.get(position).text);

        return ret;
    }

    private static class TitleViewHolder{
        TextView title;
    }

    private static class ContentViewHolder{
        TextView cont;
    }
}
